<?php
	
	if ($_SESSION['Level'] == "HRD")
	{	
		if(ISSET($_GET['Halaman']))
		{
		// Data Paging
		$halaman=$_GET['Halaman'];
		}
		$batas=5;
		if(empty($halaman))
		{
			$posisi=0;
			$halaman=1;
		}
		else
		{
			$posisi = ($halaman-1) * $batas;
		}
		
		
		// Data untuk paging
		if ($_GET['Tlc'] != "" && $_GET['Tlc'] == "ALL")
		{
			$Tlc = $_GET['Tlc'];
			$Tahun = $_GET['Tahun'];
				if ($Tahun == "ALL")
				{
				$QueryALL = "WHERE ket_approval = '1'";
				}
				else
				{
				$QueryALL = "WHERE ket_approval = '1' AND ket_tgl_cuti like '%".$Tahun."%'";
				}
			$QuerySuratCuti1 = "SELECT * FROM d_surat    	".$QueryALL."
															ORDER BY tgl_pengajuan DESC limit ".$posisi.",".$batas."";
															
			$QuerySuratCuti2 = "SELECT * FROM d_surat 		 ".$QueryALL."
															";
															
			$UntukGET = "&Tlc=".$Tlc."&Tahun=".$Tahun."&Cari=Ok";
		}
		else if ($_GET['Tlc'] != "")
		{
			$Tahun = $_GET['Tahun'];
				if ($Tahun == "ALL")
				{
				$QueryALL = "";
				}
				else
				{
				$QueryALL = "AND ket_tgl_cuti like '%".$Tahun."%'";
				}
			$Tlc = $_GET['Tlc'];
			$DataKaryUtkQuery1 = mysql_query("SELECT * FROM d_karyawan WHERE tlc = '$Tlc'");
			$DataKaryUtkQuery2 = mysql_fetch_array($DataKaryUtkQuery1);
			$IdKarUtkQuery = $DataKaryUtkQuery2['id_karyawan'];
			$TglMasukKerja = $DataKaryUtkQuery2['tgl_masuk_kerja'];
			$UntukGET = "&Tahun=".$Tahun."&Cari=Ok";
			$QuerySuratCuti1 = "SELECT * FROM d_surat WHERE id_karyawan = '".$IdKarUtkQuery."' ".$QueryALL." AND ket_approval = '1'
															OR 
															id_karyawan = 'ALL' 
															".$QueryALL."
															AND 
															tgl_pengajuan >= '$TglMasukKerja'														
															ORDER BY tgl_pengajuan DESC limit ".$posisi.",".$batas."";
															
			$QuerySuratCuti2 = "SELECT * FROM d_surat WHERE id_karyawan = '".$IdKarUtkQuery."' ".$QueryALL." AND ket_approval = '1'
															OR 
															id_karyawan = 'ALL' 
															".$QueryALL."
															AND 
															tgl_pengajuan >= '$TglMasukKerja'
															 ";
															
			$UntukGET = "&Tlc=".$Tlc."&Tahun=".$Tahun."&Cari=Ok";
		}
		else
		{
			// Untuk ditampilkan
			$QuerySuratCuti1 = "SELECT * FROM d_surat WHERE ket_approval = '1' ORDER BY tgl_pengajuan DESC limit ".$posisi.",".$batas."";
			// Untuk dihitung jumlahnya
			$QuerySuratCuti2 = "SELECT * FROM d_surat WHERE ket_approval = '1' ";
			
			$UntukGET = "";
		}
		
		// Form untuk pencarian
		echo 	"
				<table border=2>
				<tr>
				<form method=get action=index.php id='FormPencarianSuratCuti'>
				<input type=hidden name=Content value=SuratCuti> 
				<input type=hidden name=Halaman value=1> 
				<td>Tlc:</td>
				<td><input type=text name='Tlc' id='Tlc' value=$Tlc></td>
				<td>Tahun:</td>
				<td><input type=text name='Tahun' id='Tahun' value=$Tahun></td>
				<td><input type=submit name=Cari value=Ok onclick='saveForm(); return false;'></td>
				</form>
				</tr>
				</table>
				<br>
				";
		// Mengambil data surat 
		$Surat = mysql_query("$QuerySuratCuti1");

		// Menampilkan query Surat yang sudah disahkan
		echo 	"
				<table border=2 cellpadding=10>
				<tr>
				<td colspan=8  align='center'><h3>Surat Cuti</h3></td>
				</tr>
				<tr align='center'>
				<td>No</td>
				<td>Nama</td>
				<td>Jenis Surat</td>
				<td>Jumlah Cuti</td>
				<td>Keterangan Tanggal</td>
				<td>Keterangan Approval</td>
				<td>Approval</td>
				<td>-</td>
				</tr>
				";
				$No = $posisi+1;
		
		while ($Data1 = mysql_fetch_array($Surat))
			{
				// Mengambil data karyawan untuk ditampilkan
				$DataKaryawan = mysql_query("SELECT * FROM d_karyawan WHERE id_karyawan = '$Data1[id_karyawan]'");
				$Data2 = mysql_fetch_array($DataKaryawan);
					
		echo 	"
				<tr>
				<td>$No.</td>
				<td>$Data2[nama_lengkap]</td>
				<td>";
				$JenisSurat = $Data1['jenis_surat'];
				// Mengubah jenis surat
				include("config/jenis_surat.php");
		echo	"
				$JenisSurat</td>
				<td>$Data1[jumlah_cuti]</td>
				<td>";
				// Memecah tanggal string ke array
				$KetTgl = explode("-", $Data1['ket_tgl_cuti']);
				for ($i=0;$i<=$Data1['jumlah_cuti']-1;$i++)
				{
					echo "$KetTgl[$i]<br>";
				}		
		echo 	"
				</td>
				<td>";
				$KetApproval = $Data1['ket_approval'];
				// Mengubah ket approval
				include("config/ket_approval.php");
		echo	"
				$KetApproval</td>
				";
				// Mengambil data approval
				$DataApproval = mysql_query("SELECT * FROM d_karyawan WHERE id_karyawan = '$Data1[id_approval]'");
				$Data3= mysql_fetch_array($DataApproval);
		echo 	"
				<td>$Data3[tlc]</td>
				<td><a href = 'index.php?Content=LihatSurat&IdSurat=$Data1[id_surat]' target='_blank'> Lihat </a>
					<a href = 'index.php?Content=PDF&IdSurat=$Data1[id_surat]'&IdKaryawan='$Data2[id_karyawan]' target='_blank'> Report </a>";

				if($Data1['ket_approval'] == "1" && $Data1['jenis_surat'] == "1" || $Data1['jenis_surat'] == "2" || $Data1['jenis_surat'] == "4")
				{
		echo	"
				<a href = 'index.php?Content=EditSurat&IdSurat=$Data1[id_surat]' > Edit </a>				
				";
				}
		echo 	"
				</td>
				</tr>
				";
				$No++;
			}
			
		echo 	"
				</table>
				";
				
		// Untuk Url
		$file="index.php?Content=SuratCuti";
		
		$hasil2=mysql_query($QuerySuratCuti2);
		$jmldata=mysql_num_rows($hasil2);
		$jmlhalaman=ceil($jmldata/$batas);

			//link ke halaman sebelumnya (previous)
			if($halaman > 1)
			{
				$previous=$halaman-1;
				echo "<A HREF=$file&Halaman=1$UntukGET><< First</A> | 
					<A HREF=$file&Halaman=$previous$UntukGET>< Previous</A> | ";
			}
			else
			{ 
				echo "<< First | < Previous | ";
			}
			
			// Untuk angka pada halaman
			$angka=($halaman > 3 ? " ... " : " ");
			for($i=$halaman-2;$i<$halaman;$i++)
			{
			  if ($i < 1) 
				  continue;
			  $angka .= "<a href=$file&Halaman=$i$UntukGET>$i</A> ";
			}

			$angka .= " <b>$halaman</b> ";
			for($i=$halaman+1;$i<($halaman+3);$i++)
			{
			  if ($i > $jmlhalaman) 
				  break;
			  $angka .= "<a href=$file&Halaman=$i$UntukGET>$i</A> ";
			}

			$angka .= ($halaman+2<$jmlhalaman ? " ...  
					  <a href=$file&Halaman=$jmlhalaman$UntukGET>$jmlhalaman</A> " : " ");

			echo "$angka";

			//link kehalaman berikutnya (Next)
			if($halaman < $jmlhalaman)
			{
				$next=$halaman+1;
				echo " | <A HREF=$file&Halaman=$next$UntukGET>Next ></A> | 
			  <A HREF=$file&Halaman=$jmlhalaman$UntukGET>Last >></A> ";
			}
			else
			{ 
				echo " | Next > | Last >>";
			}
	echo 	"
			<p>Ditemukan <b>$jmldata</b> surat</p>
			";
			
	}
?>